package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcExpenseTypeModelCodeMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcExpenseItem;
import com.csun.cmny.provider.model.domain.OmcExpenseType;
import com.csun.cmny.provider.model.domain.OmcExpenseTypeModelCode;
import com.csun.cmny.provider.model.domain.OmcOldmanExpenseItem;
import com.csun.cmny.provider.model.dto.ExpenseTypeModelCodeDto;
import com.csun.cmny.provider.model.dto.revise.GroupPaymentFeeQueryDto;
import com.csun.cmny.provider.model.vo.ExpenseTypeModelCodeVo;
import com.csun.cmny.provider.model.vo.revise.GroupPaymentFeeVo;
import com.csun.cmny.provider.service.OmcExpenseItemService;
import com.csun.cmny.provider.service.OmcExpenseTypeModelCodeService;
import com.csun.cmny.provider.service.OmcExpenseTypeService;
import com.csun.cmny.provider.service.OmcOldmanExpenseItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;


@Slf4j
@Service
@Transactional
public class OmcExpenseTypeModelCodeServiceImpl extends BaseService<OmcExpenseTypeModelCode>
        implements OmcExpenseTypeModelCodeService {

    @Resource
    private OmcExpenseTypeModelCodeMapper omcExpenseTypeModelCodeMapper;

    @Resource
    private OmcExpenseTypeService omcExpenseTypeService;

    @Resource
    private OmcExpenseItemService omcExpenseItemService;

    @Resource
    private OmcOldmanExpenseItemService omcOldmanExpenseItemService;

    @Override
    public Integer addExpenseTypeModelCode(List<OmcExpenseTypeModelCode> expenseTypeModelCodeList, LoginAuthDto loginAuthDto) {

        if (expenseTypeModelCodeList != null && expenseTypeModelCodeList.size() > 0) {

            for (OmcExpenseTypeModelCode item : expenseTypeModelCodeList) {
                item.setUpdateInfo(loginAuthDto);
                if(item.getGroupId() == null){
                    item.setGroupId(loginAuthDto.getGroupId());
                }
                //校验费用类别是否已经添加过了
                checkExpenseTypeRepeat(item);
                //校验缴费类型是否存在
                checkExpenseTypeExist(item);

                //如果是添加为入院项目，要校验该类别下的消费项目的消费方式是否是 元/一次性
                if(item.getModelCode().equals(1)){
                    OmcExpenseItem itemQuery = new OmcExpenseItem();
                    itemQuery.setExpenseTypeId(item.getExpenseTypeId().intValue());
                    List<OmcExpenseItem> itemList = omcExpenseItemService.select(itemQuery);
                    if(itemList != null && itemList.size() > 0){
                        for(OmcExpenseItem expenseItem : itemList){
                            if(!expenseItem.getCostType().equals(5)){
                                throw new OmcBizException(ErrorCodeEnum.OMC10037040);
                            }
                        }
                    }
                }
            }
        }
        return batchSave(expenseTypeModelCodeList);
    }

    @Override
    public Integer changeIsAdjust(Long id) {

        OmcExpenseTypeModelCode omcExpenseTypeModelCode = omcExpenseTypeModelCodeMapper.selectByPrimaryKey(id);
        if (omcExpenseTypeModelCode == null) {
            throw new OmcBizException(ErrorCodeEnum.OMC10037014);
        }
        switch (omcExpenseTypeModelCode.getIsAdjust()) {
            case 0:
                omcExpenseTypeModelCode.setIsAdjust(1);
                break;
            case 1:
                omcExpenseTypeModelCode.setIsAdjust(0);
                break;
        }
        return omcExpenseTypeModelCodeMapper.updateByPrimaryKeySelective(omcExpenseTypeModelCode);
    }

    @Override
    public Integer changeIsOutReturn(Long id) {
        OmcExpenseTypeModelCode omcExpenseTypeModelCode = omcExpenseTypeModelCodeMapper.selectByPrimaryKey(id);
        if (omcExpenseTypeModelCode == null) {
            throw new OmcBizException(ErrorCodeEnum.OMC10037014);
        }
        switch (omcExpenseTypeModelCode.getIsOutReturn()) {
            case 0:
                omcExpenseTypeModelCode.setIsOutReturn(1);
                break;
            case 1:
                omcExpenseTypeModelCode.setIsOutReturn(0);
                break;
        }
        return omcExpenseTypeModelCodeMapper.updateByPrimaryKeySelective(omcExpenseTypeModelCode);
    }

    @Override
    public Integer changeIsLeaveReturn(Long id) {
        OmcExpenseTypeModelCode omcExpenseTypeModelCode = omcExpenseTypeModelCodeMapper.selectByPrimaryKey(id);
        if (omcExpenseTypeModelCode == null) {
            throw new OmcBizException(ErrorCodeEnum.OMC10037014);
        }
        switch (omcExpenseTypeModelCode.getIsLeaveReturn()) {
            case 0:
                omcExpenseTypeModelCode.setIsLeaveReturn(1);
                break;
            case 1:
                omcExpenseTypeModelCode.setIsLeaveReturn(0);
                break;
        }
        return omcExpenseTypeModelCodeMapper.updateByPrimaryKeySelective(omcExpenseTypeModelCode);
    }

    @Override
    public List<ExpenseTypeModelCodeVo> queryAll(ExpenseTypeModelCodeDto expenseTypeModelCodeDto, LoginAuthDto loginAuthDto) {

        Long groupId = loginAuthDto.getGroupId();
        if(expenseTypeModelCodeDto.getGroupId() != null){
            groupId = expenseTypeModelCodeDto.getGroupId();
        }
        List<ExpenseTypeModelCodeVo> expenseTypeModelCodeVoList = omcExpenseTypeModelCodeMapper.queryAll(groupId);
        return expenseTypeModelCodeVoList;
    }


    /**
     * 根据项目ID和机构ID查询项目是否请假退费
     *
     * @param itemId
     * @param groupId
     * @return false 请假不退费  true 请假退费
     */
    @Override
    public boolean isLeaveReturn(Long itemId, Long groupId) {
        int n = omcExpenseTypeModelCodeMapper.selectLeaveReturn(itemId,groupId);
        if(n>0){
            return false;
        }
        return true;
    }

    /**
     * query group payment fee by group id
     *
     * @param groupPaymentFeeQueryDto the group payment fee query dto
     * @return the list
     */
    @Override
    public List<GroupPaymentFeeVo> queryGroupPaymentFeeByGroupId(GroupPaymentFeeQueryDto groupPaymentFeeQueryDto) {

        return omcExpenseTypeModelCodeMapper.queryGroupPaymentFeeByGroupId(groupPaymentFeeQueryDto);
    }

    @Override
    public Integer deleteById(Long id) {

        OmcExpenseTypeModelCode modelCode = selectByKey(id);
        OmcOldmanExpenseItem oldmanExpenseItem = new OmcOldmanExpenseItem();
        oldmanExpenseItem.setExpenseTypeId(modelCode.getExpenseTypeId());
        int ret = omcOldmanExpenseItemService.selectCount(oldmanExpenseItem);
        if(ret > 0){
            throw new OmcBizException(ErrorCodeEnum.OMC10037042);
        }
        return deleteByKey(id);
    }

    public void checkExpenseTypeRepeat(OmcExpenseTypeModelCode expenseTypeModelCode) {
        Example example = new Example(OmcExpenseTypeModelCode.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("expenseTypeId", expenseTypeModelCode.getExpenseTypeId());
        criteria.andEqualTo("groupId", expenseTypeModelCode.getGroupId());
        int ret = selectCountByExample(example);
        if (ret > 0) {
            throw new OmcBizException(ErrorCodeEnum.OMC10037013);
        }
    }

    public void checkExpenseTypeExist(OmcExpenseTypeModelCode expenseTypeModelCode) {
        Example example = new Example(OmcExpenseType.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", expenseTypeModelCode.getExpenseTypeId());
        criteria.andEqualTo("groupId", expenseTypeModelCode.getGroupId());
        int result = omcExpenseTypeService.selectCountByExample(example);
        if (result != 1) {
            throw new OmcBizException(ErrorCodeEnum.OMC10037004);
        }
    }
}
